<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<title>ASP.NET Objects</title>
<script language="VB" runat="server">
Sub MyDataGrid_EditCommand(s As Object, e As DataGridCommandEventArgs )
	MyDataGrid.EditItemIndex = e.Item.ItemIndex
	BindData
End Sub
Sub MyDataGrid_Cancel(Source As Object,   E As DataGridCommandEventArgs)
	MyDataGrid.EditItemIndex = -1
	BindData()
End Sub
Sub MyDataGrid_UpdateCommand(s As Object, e As DataGridCommandEventArgs )
	Dim myConn As SqlConnection
	Dim MyCommand As SqlCommand
	Dim strConn as string = _
	ConfigurationSettings.appSettings("defaultcon")
	Dim txtFirstName As textbox = E.Item.cells(2).Controls(0)
	Dim txtLastName As textbox = E.Item.cells(3).Controls(0)
	Dim txtTitle As textbox = E.Item.cells(4).Controls(0)
	Dim strUpdateStmt As String
        strUpdateStmt =" UPDATE Employees SET" & _
        " FirstName =@Fname, LastName =@Lname, Title = @Title " & _
        " WHERE EmployeeID = @EmpID"
	myConn = New SqlConnection(strConn)
	MyCommand = New SqlCommand(strUpdateStmt, myConn)
	MyCommand.Parameters.Add(New SQLParameter("@Fname", txtFirstName.text))
	MyCommand.Parameters.Add(New SQLParameter("@Lname", txtLastName.text))
	MyCommand.Parameters.Add(New SQLParameter("@Title", txtTitle.text))
	MyCommand.Parameters.Add(New SQLParameter("@EmpID", e.Item.Cells(1).Text ))
	myConn.Open()
	MyCommand.ExecuteNonQuery()
	MyDataGrid.EditItemIndex = -1
        	myConn.close
	BindData
End Sub

Sub Page_Load(Source as Object, E as EventArgs)
	if not Page.IsPostBack then
		BindData
	end if
End Sub
Sub BindData()
Dim strConn as string = _
ConfigurationSettings.appSettings("defaultcon")
Dim strSQL as string = "Select EmployeeID, FirstName, LastName, Title from Employees"
  Dim myConn As New SQLConnection(strConn)
  Dim objDR as SQLDataReader
  Dim Cmd as New SQLCommand(strSQL , myConn)
  myConn.Open()
  objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
  MyDataGrid.DataSource = objDR
  MyDataGrid.DataBind()
  myConn.close
End Sub
</script>
</head>
<body><div align="center">
<h2>Edit and Update Data</h2>
<form runat="server" method="post">
<asp:Datagrid runat="server"
	Id="MyDataGrid"
	GridLines="Both"
	cellpadding="0"
	cellspacing="0"
	Headerstyle-BackColor="gray"
	Headerstyle-Font-Name="Arial"
	Headerstyle-Font-Size="10"
        	Headerstyle-Font-Bold="True"
	BackColor="#FFFFFF"
	Font-Name="Arial"
	Font-Size="10"
	BorderColor="Black"	
	AutogenerateColumns="False"
	OnEditcommand="MyDataGrid_EditCommand"
	OnCancelcommand="MyDataGrid_Cancel"
	OnUpdateCommand="MyDataGrid_UpdateCommand">
	
	<Columns>
		<asp:EditCommandColumn 
			ButtonType="LinkButton" 
			UpdateText="Update" 
			CancelText="Cancel" 
			EditText="<IMG SRC='/images/Edit.gif' 
			Border=0 Width=12 Height=12>"
                                          ItemStyle-HorizontalAlign="Center" 
		              HeaderText="&nbsp;Edit&nbsp;">
		</asp:EditCommandColumn>		
		<asp:BoundColumn 
			DataField="EmployeeID" 
			HeaderText="&nbsp;EmployeeID&nbsp;" 
			ReadOnly="True">
		</asp:BoundColumn>
		<asp:BoundColumn 
			DataField="FirstName" 
			HeaderText="&nbsp;FirstName&nbsp;">
		</asp:BoundColumn>
		<asp:BoundColumn 
			DataField="LastName" 
			HeaderText="&nbsp;LastName&nbsp;">
		</asp:BoundColumn>
		<asp:BoundColumn 
			DataField="Title" 
			HeaderText="&nbsp;Title&nbsp;">
		</asp:BoundColumn>
	</Columns>
</asp:DataGrid>
</form>
</div>
</body>
</html>
